class CLock
{
protected:
	CRITICAL_SECTION                    m_cs;

public:
	CLock(){
		::InitializeCriticalSection(&m_cs); 
	}
	~CLock()
	{
		::DeleteCriticalSection(&m_cs);
	}

public:
	void Lock()
	{
		::EnterCriticalSection(&m_cs); 
	}
	void Unlock()
	{ 
		::LeaveCriticalSection(&m_cs); 
	}
};


template<class T> 
class CTLock{  
private:  
    T *m_pLock;  
public:  
	CTLock(T *L){
        m_pLock=L;  
        m_pLock->Lock();  
    }  
	~CTLock(){
        if(m_pLock!=NULL){  
            m_pLock->Unlock();  
        }  
    }  
}; 

typedef CTLock<CLock> CBoxLock;